import 'package:cunyutech_flutter_ui/src/text/main.dart';
import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class PrimaryButtonProps {
  final String title;
  EdgeInsetsGeometry margin;
  double borderRadius;
  Color backgroundColor;
  double height;
  VoidCallback onTap;
  Color color;
  double fontSize;
  bool bold;
  double width;
  PrimaryButtonType type;
  bool loading;
  bool disabled;

  PrimaryButtonProps({this.title, this.fontSize, this.bold = true, this.color, this.type = PrimaryButtonType.PRIMARY, this.loading = false, this.disabled = false, this.onTap, this.margin, this.width, this.borderRadius, this.backgroundColor, this.height});
  PrimaryButtonProps.cancle({
    this.title = "取消", this.fontSize, this.bold = true, this.color, this.type = PrimaryButtonType.DEFAULT, this.loading = false, this.disabled = false, this.onTap, this.margin, this.width, this.borderRadius, this.backgroundColor, this.height
  });
}

enum PrimaryButtonType {
  PRIMARY,
  DEFAULT,
}

class PrimaryButton extends StatelessWidget{
  static const double ButtonHeight = 49;
  static const EdgeInsetsGeometry ButtonMargin = EdgeInsets.symmetric(horizontal: CYMargin.Default);
  static const double ButtonBorderRadius = 8;
  static const double SmallButtonBorderRadius = 8;
  static const double XSmallButtonBorderRadius = 5;

  final String title;
  final EdgeInsetsGeometry margin;
  final double borderRadius;
  final Color backgroundColor;
  final double height;
  final VoidCallback onTap;
  final Color color;
  final double fontSize;
  final bool bold;
  final double width;
  final PrimaryButtonType type;
  final bool loading;
  final bool disabled;


  const PrimaryButton({
    Key key, 
    this.width, 
    this.bold = true, 
    this.color, 
    this.fontSize,
    this.onTap, 
    this.height = ButtonHeight, 
    this.backgroundColor, 
    this.title = "确定", 
    this.margin = ButtonMargin, 
    this.borderRadius =  ButtonBorderRadius,
    this.disabled = false,
    this.loading = false,
    this.type = PrimaryButtonType.PRIMARY
  }) : super(key: key);

  PrimaryButton.cancel(PrimaryButtonProps props):
    this.onTap = props.onTap,
    this.bold = props.bold ?? true,
    this.height = props.height ?? ButtonHeight, 
    this.color = props.color,
    this.fontSize = props.fontSize,
    this.backgroundColor = props.backgroundColor, 
    this.title = props.title ?? "取消", 
    this.margin = props.margin ?? ButtonMargin, 
    this.width = props.width,
    this.disabled = false,
    this.loading = false,
    this.type = PrimaryButtonType.DEFAULT,
    this.borderRadius =  props.borderRadius ?? ButtonBorderRadius;


  PrimaryButton.withProps(PrimaryButtonProps props):
    this.onTap = props.onTap,
    this.bold = props.bold ?? true,
    this.height = props.height ?? ButtonHeight, 
    this.color = props.color,
    this.fontSize = props.fontSize,
    this.backgroundColor = props.backgroundColor, 
    this.title = props.title ?? "确定", 
    this.margin = props.margin ?? ButtonMargin, 
    this.width = props.width,
    this.type = props.type ?? PrimaryButtonType.PRIMARY,
    this.disabled = props.disabled ?? false,
    this.loading = props.loading ?? false,
    this.borderRadius =  props.borderRadius ?? ButtonBorderRadius;
  
  @override
  Widget build(BuildContext context) {
    var cs = CYThemeData.of(context);

    var defaultBackgroundColor = type == PrimaryButtonType.PRIMARY ? cs.primaryColor : cs.backColorSystem.secondaryColor;
    var defaultColor = type == PrimaryButtonType.PRIMARY ? cs.darkForeColorSystem.primaryColor : cs.primaryForeColor;
    var buttonBackgroundColor = backgroundColor ?? defaultBackgroundColor;
    var buttonColor = color ?? defaultColor;
    
    Widget titleWidget = CYTitle(
      title,
      color: color ?? cs.darkForeColorSystem.primaryColor,
      alignment: Alignment.center,
      bold: bold,
      fontSize: fontSize,
    );

    if(loading == true) {
      final Widget loading = SpinKitFadingCircle(color: buttonColor, size: 22);
      titleWidget = Container(
        alignment: Alignment.center,
        child: loading
      );
    }

    Widget content = Container(
      height: height,
      width: width,
      decoration: BoxDecoration(
        color: buttonBackgroundColor,
        borderRadius: BorderRadius.circular(borderRadius)
      ),
      margin: margin,
      alignment: Alignment.center,
      child: titleWidget
    );

    if(disabled == true) {
      return Opacity(
        opacity: 0.5,
        child: content,
      );
    }
    
    return GestureDetector(
      onTap: onTap,
      child: content
    );
  }
}